package dk.iha;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public class BatteryIndicatorSensor implements ISensor {

	float batteryLevel = 0;
	Context context;
	boolean isRunning=false;
	
	BatteryIndicatorSensor(Context ctx){
		context = ctx;
	}
	/**
	 * Returns the battery level in percent
	 */
	public float getBatteryLevel() {
		return batteryLevel; 
	}
	
	public void start() {
		IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent batteryStatus = context.registerReceiver(null, ifilter);
		int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
		int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
		batteryLevel = (float)100.0 * (float)level / (float)scale;	
		 //Log.w("WL", "BatteryIndicatorSensor:"+Float.toString(batteryLevel));
		isRunning=true;
	}

	public void stop() {
		isRunning=false;
	}

	public boolean isRunning() {
		return isRunning;
	}

}
